﻿<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:Sample.ViewModels;assembly=Sample.ViewModels"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:r="clr-namespace:Codeplex.Reactive.Interactivity;assembly=ReactiveProperty.NET45"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" x:Class="WPF.Views.EventToReactive"
        Title="EventToReactive" Height="300" Width="300">
    <!-- recommend App.xaml.cs global resource -->
    <Window.Resources>
        <vm:Converters x:Key="Converters" />
        <Storyboard x:Key="AlertStoryboard">
        	<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="border">
        		<DiscreteObjectKeyFrame KeyTime="0">
        			<DiscreteObjectKeyFrame.Value>
        				<Visibility>Visible</Visibility>
        			</DiscreteObjectKeyFrame.Value>
        		</DiscreteObjectKeyFrame>
        		<DiscreteObjectKeyFrame KeyTime="0:0:0.5">
        			<DiscreteObjectKeyFrame.Value>
        				<Visibility>Visible</Visibility>
        			</DiscreteObjectKeyFrame.Value>
        		</DiscreteObjectKeyFrame>
        		<DiscreteObjectKeyFrame KeyTime="0:0:2">
        			<DiscreteObjectKeyFrame.Value>
        				<Visibility>Hidden</Visibility>
        			</DiscreteObjectKeyFrame.Value>
        		</DiscreteObjectKeyFrame>
        	</ObjectAnimationUsingKeyFrames>
        	<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="border">
        		<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
        		<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
        		<EasingDoubleKeyFrame KeyTime="0:0:2" Value="0.1"/>
        	</DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Window.DataContext>
        <vm:EventToReactiveViewModel />
    </Window.DataContext>

	<i:Interaction.Triggers>
		<i:EventTrigger EventName="PropertyChanged" SourceObject="{Binding AlertMessage, Mode=OneWay}">
			<ei:ControlStoryboardAction Storyboard="{StaticResource AlertStoryboard}"/>
		</i:EventTrigger>
	</i:Interaction.Triggers>

	<Grid>

		<Grid>
			<!-- Use Blend SDK's Interaction Trigger -->
			<!-- Event binding to ReactiveProperty -->
			<i:Interaction.Triggers>
				<i:EventTrigger EventName="MouseMove">
					<!-- ConvertBack function is Func<object, object>-->
					<r:EventToReactive ReactiveProperty="{Binding MouseMove}" 
						Converter="{Binding MouseEventToPoint, Source={StaticResource Converters}}" />
				</i:EventTrigger>
				<i:EventTrigger EventName="MouseDown">
					<!-- direct event bind -->
					<r:EventToReactive ReactiveProperty="{Binding MouseDown}" IgnoreEventArgs="True" />
				</i:EventTrigger>
				<i:EventTrigger EventName="MouseEnter">
					<!-- IgnoreEventArgs = true send Unit -->
					<r:EventToReactive ReactiveProperty="{Binding MouseEnter}" IgnoreEventArgs="true" />
				</i:EventTrigger>
			</i:Interaction.Triggers>
			<TextBlock Text="{Binding CurrentPoint.Value}" />
			<TextBlock Text="{Binding Entered.Value}" Margin="0,100,0,0" />
		</Grid>
		<Border x:Name="border" BorderBrush="Black" BorderThickness="1" Background="#FFF0B2B2" Visibility="Hidden">
			<TextBlock TextWrapping="Wrap" Text="{Binding AlertMessage.Value}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
		</Border>
	</Grid>
</Window>